<template>
  <div>
    <el-button style="float:right" type="primary" @click="submitForm">保存</el-button>
    <div style="margin-top:20px" class="qup-title-box"><span class="border" /><span>审核结果</span></div>
    <el-form
      ref="refForm"
      v-loading="editLoading"
      label="left"
      :rules="formRules"
      :model="formData"
      size="mini"
      label-width="140px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="审核结果:" prop="reviewResult">
            <el-select
              v-model="formData.reviewResult"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in auditReusltOptions"
                :key="item.dictCode"
                :label="item.dictName"
                :value="item.dictCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="审核意见:" prop="reviewAdvice">
            <el-input v-model="formData.reviewAdvice" maxlength="1000" show-word-limit type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-loading="dataLoading">
      <!-- 变更详情 -->
      <base-info :detail-data="detailData" />
      <!-- 返利政策变更信息 -->
      <change-info :detail-data="detailData" :change-data="changeData" />
    </div>
  </div>
</template>
<script>
import { nowTime } from '@/utils/tool'
import { getDetailByApplyCode, saveAudit, getDetailById } from '@/api/shopChangeRule'
import { getDictByPatchType } from '@/api/dataDict'
import changeInfo from './component/rebateRuleChangeInfo.vue'
import baseInfo from '../component/rebateBaseInfo.vue'
import BackPageMixin from '@/mixins/backPage'
export default {
  name: 'RebateChangeAudit',
  components: { baseInfo, changeInfo },
  mixins: [BackPageMixin],
  props: {
    // id: {
    //   type: String,
    //   default: ''
    // },
    // applyCode: {
    //   type: String,
    //   default: ''
    // }
  },
  data() {
    return {
      nowTime,
      dataLoading: false,
      editLoading: false,
      saveLoading: false,
      id: '',
      applyCode: '',
      auditReusltOptions: [], // 审核结果
      formData: {
        id: '',
        reviewResult: '',
        reviewAdvice: '',
        changeDesc: '',
        shopAccounting: '',
        dheadAccounting: '',
        isMonthReturn: '',
        applyCode: '',
        applicantCode: '',
        applicantName: '',
        applicantTime: nowTime()
      },
      baseInfo: {},
      detailData: {},
      changeData: {},
      formRules: {
        reviewResult: [{ required: true, message: '请选择审核结果', trigger: 'change' }]
      }
    }
  },
  created() {
    this.id = this.$route.query.id
    this.applyCode = this.$route.query.applyCode
    if (this.applyCode) {
      this.dataLoading = true
      getDetailByApplyCode(this.applyCode).then(res => {
        if (res.success === 1) {
          this.detailData = res.data
          this.dataLoading = false
        }
      })
    }
    if (this.id) {
      this.editLoading = true
      getDetailById(this.id).then(res => {
        if (res.success === 1) {
          this.formData.changeDesc = res.data.changeDesc
          this.formData.shopAccounting = res.data.shopAccounting
          this.formData.dheadAccounting = res.data.dheadAccounting
          this.formData.isMonthReturn = res.data.isMonthReturn
          this.formData.applyCode = res.data.applyCode
          this.formData.applicantCode = this.$store.getters.user.data.account
          this.formData.applicantName = this.$store.getters.user.data.userName
          const { changeDesc, changeTime, createBy, dheadAccounting, isMonthReturn, shopAccounting, beforeShopAccounting, beforeDheadAccounting, beforeIsMonthReturn } = res.data
          this.changeData = { changeDesc, changeTime, createBy, dheadAccounting, isMonthReturn, shopAccounting, beforeShopAccounting, beforeDheadAccounting, beforeIsMonthReturn }
          this.editLoading = false
        }
      }).catch(() => {
        this.editLoading = false
      })
    }
    this.getDictByPatchTypeAll()
  },
  methods: {
    async getDictByPatchTypeAll() {
      const { REVIEW_RESULT } =
       await getDictByPatchType(['REVIEW_RESULT'])
      this.auditReusltOptions = REVIEW_RESULT
    },
    submitForm() {
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          this.formData.id = this.id
          this.formData.applicantTime = nowTime()
          saveAudit(this.formData).then(res => {
            if (res.success === 1) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.$store.dispatch('views/goBack')// 返回列表页
              this.saveLoading = false
            }
          }).catch(() => {
            this.saveLoading = false
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/ .el-breadcrumb__inner{
    color: $qup-color-primary;
  }
</style>
